|
If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
Datasheet File OCR Text: |
may 2013 docid18304 rev 1 1/38 AN3322 application note watt-hour meter based on th e stm32f101 microcontroller by ludek holoubek introduction this document describes, in detail, the hardware and software implementation of a watt- hour meter using the stm32f101 microcontroller. this cost effective watt-hour meter uses shunt with an operational amplifier as a current sensor, an embedded 12-bit adc for current and voltage measurement, gpio for lcd management, and a lot of other peripherals for communication, tamper detection, keyboar d, and power disconnection. powerful architecture of the stm32? microcontroller allows sampling at 1 msps. the high sampling rate makes it possible to use methods for adc resolution enhancement. main features ? metrological parts ? microcontroller and external op amp only ? compliant with en 50470-1:2006 class b, precision better than 1% ? starting current 20 ma, nominal current 5 a, maximum current 100 a ? reads the voltage and the current signal up to the 21 st harmonic ? shunt as a current sensor. ct and rogowski coil is optional ? additional parts included in reference design ? lcd driven directly by microcontroller ? keyboard driven directly by microcontroller ? an extra external eeprom for user data ? bi-stable relay, ir communication, serial communication ? backup battery, complete power meter power management www.st.com
contents AN3322 2/38 docid18304 rev 1 contents 1 watt-hour meter using th e stm32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.1 stm32 adc parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.2 requirements for meter in class b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 definitions of the power meter parameters (according to en 50407-1:2006) . . . . . 7 1.3 stm32 adc dynamic measurement range . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3.1 voltage measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3.2 there are two current ranges in the meter . . . . . . . . . . . . . . . . . . . . . . . 9 1.4 error sources on the stm32 meter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.5 linearity of stm32 adc and its inl reduction . . . . . . . . . . . . . . . . . . . . . 10 1.6 thermal noise present on stm32 meter platform . . . . . . . . . . . . . . . . . . 10 1.7 crosstalk between subsequent multiplexed channels . . . . . . . . . . . . . . . .11 1.8 power and energy measurement by stm32 embedded adc . . . . . . . . . 13 1.9 adc enhancement by oversampling technique . . . . . . . . . . . . . . . . . . . . 15 1.10 output values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 some reference values and constants are defined . . . . . . . . . . . . . . . . . . . . . . . . 16 1.10.1 rms voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.10.2 rms current - high gained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.10.3 rms current - low gained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.10.4 power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.10.5 energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.10.6 pulse output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 computational issues and compensation al gorithms . . . . . . . . . . . . . 22 2.1 non-coherent sampling issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.2 timer evaluation for coherent sampling triggering . . . . . . . . . . . . . . . . . . 24 2.3 timer setting constraints for coherent sa mpling triggering . . . . . . . . . . . . 25 2.4 precise timer setting for coherent sampling . . . . . . . . . . . . . . . . . . . . . . . 26 2.5 frequency measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.6 filtering of input signal for proper zero crossing detection . . . . . . . . . . . . 29 docid18304 rev 1 3/38 AN3322 contents 2.7 phase shift correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.7.1 phase shift measured for the stm32 meter . . . . . . . . . . . . . . . . . . . . . 30 2.7.2 phase shift correction - error evaluation . . . . . . . . . . . . . . . . . . . . . . . . 30 2.7.3 phase shift correction - filter implementation . . . . . . . . . . . . . . . . . . . . . 33 2.8 calibration procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.9 reactive and apparent power computation . . . . . . . . . . . . . . . . . . . . . . . 34 2.10 analog part schematic - part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.11 analog part schematic - part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3 references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4 revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 list of tables AN3322 4/38 docid18304 rev 1 list of tables table 1. stm32 adc parameter specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 table 2. stm32 adc input characteristics specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 table 3. crosstalk between subsequent multiplexed channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 table 4. precision vs. timer setting. simulated on signal u (t) = sin (2 x pi x 50 x t). . . . . . . . . . . . . 24 table 5. high speed internal (hsi) rc oscilla tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 6 table 6. error of instant power caused by phase shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 table 7. document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 docid18304 rev 1 5/38 AN3322 list of figures list of figures figure 1. requirements for class b power meter precision, three ranges . . . . . . . . . . . . . . . . . . . . . 7 figure 2. template used to check meter accuracy (up to i max ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 figure 3. typical thermal noise present on stm32 meter platform. histogram shows noise equivalent to white gaussian noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 figure 4. internal and external analog input pin capacitors and resistors . . . . . . . . . . . . . . . . . . . . . 12 figure 5. complete block diagram of the algorithm used in stm32 power meter . . . . . . . . . . . . . . . 13 figure 6. voltage circuitry of stm32 meter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 figure 7. current circuitry of stm32 meter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 figure 8. current circuitry of stm32 meter, op amp satu ration marked by red circle . . . . . . . . . . . . 16 figure 9. voltage circuitry - calibration c u constant computation . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 figure 10. current circuitry - calibration c i constant computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 figure 11. instant power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 figure 12. parasitic frequency fh in detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 figure 13. error of u rms vs. the timer error in timer ticks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 figure 14. error of u rms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 figure 15. finer calculation of zero cro ssing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 figure 16. voltage signal crossing zero voltage line twice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 figure 17. voltage signal used for correct zero crossing af ter proper filtering. . . . . . . . . . . . . . . . . . . 29 figure 18. zero crossing for green - u, blue - ilg, red- ihg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 figure 19. error of instant power for pf = 1 and pf = 0.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 figure 20. error of instant power caused by phase shift for pf = 1. . . . . . . . . . . . . . . . . . . . . . . . . . . 32 figure 21. error of instant power caused by phase shift for pf = 0.5 . . . . . . . . . . . . . . . . . . . . . . . . . 33 figure 22. analog part schematic - part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 figure 23. analog part schematic - part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 watt-hour meter using the stm32 AN3322 6/38 docid18304 rev 1 1 watt-hour meter using the stm32 the following sections describe the necessary feasibility study, theory and computation for the stm32 power meter implementation. the system design, stm32 parameter evaluation, adc feature enhancement, and international standards are also covered here. 1.1 stm32 adc parameters the most important part of the power meter and its microcontroller used in metering applications is the embedded adc. its parameters are the key to deciding if the requirements for precision of the implemented meter can be fu lfilled (international standards mentioned in these sections). the first step is to evaluate the parameters given in the adc specifications: for the given stm32 adc, the specifications and tests show: equation 1 integral non-linearity error is . equation 2 the resolution of the 12-bit adc is = 0.02% of the full scale adc range. equation 3 if , then the accuracy = resolution of given adc. equation 4 the accuracy of the 12-bit adc is = 0.02% of the full scale adc range. 1.2 requirements fo r meter in class b the european standard defining the requirements for electricity metering equipment should be the source of constraints and limits for the design presented in this application note. en 50470-1:2006 and en 50470-3:2006 are refer enced in this case. all the computation is directed to the precision of class b. table 1. stm32 adc parameter specifications symbol parameter test conditions typ. max. unit et total unadjusted error f pclk2 = 24 mhz, f adc = 12 mhz, r ain < 10 k ? , v dda = 3 to 3.6 v t a = 25 c measurements made after adc calibration v ref+ = v dda 1.3 2 lsb eo offset error 1 1.5 eg gain error 0.5 1.5 ed differential linearity error 0.7 1 el integral linearity error 0.8 1.5 e inl 12 bit ? 0,8lsb 12 bit ? = 1 2 12 -------- e inl 12 bit ? 1lsb ? -------- docid18304 rev 1 7/38 AN3322 watt-hour meter using the stm32 definitions of the power meter para meters (according to en 50407-1:2006) rms values indicated i - current - the electrical current flowing through the meter. i st - starting current - the lowest value of the current at which the mete r is declared to register active electrical energy at unity power factor. i min - minimum current - the lowest value of the current at which the european standard specifies accuracy requirements. at and above i min , up to i tr , relaxed accura cy requirements apply. i tr - transitional current - the value of the current at, and above which, up to i max , full accuracy requirements of the european standard apply. i ref - reference current - for direct meters, 10 times the transitional current. (this value is the same as the basic current, i b defined in the en 62052-11, chapter 3.5.1.2.) for current transformer operated meters, 20 times the transitional current. (the value is the same as rated current i n .) i max - maximum current - the highest value of current at which the meter purports to meet the accuracy requirements of this europe an standard (en 62052-11, chapter 3.5.2.). i tr - can be chosen (for class b) from the set of values: 0.5, 1, 1.5 and 2 a example 1 example of one setup for a meter i tr - chosen 0.5 a ist = 0.04 i tr = 20 ma i min = 0.5 i tr = 250 ma i max > 50 i tr , i max > 25 a figure 1 shows the prescribed precision in given current intervals. figure 1. requirements for class b power meter precision, three ranges , v w ? , p l q p $ p $ q r w g h i l q h g , p l q ? , w u p $ $ , w u ? , u h i ? , p d [ $ $ ! $ 0 watt-hour meter using the stm32 AN3322 8/38 docid18304 rev 1 figure 2. template used to check meter accuracy (up to i max ) 1.3 stm32 adc dynamic measurement range the precision and resolution of the adc de termines the dynamic range of measured voltage and current. this sect ion analyses whether the given adc is capable of covering, with its precision and resolution, the whol e range for requested maximum current and voltage. the input current and voltage are scaled to the full scale of the stm32 adc: ? by a voltage divider for voltage measurement ? by a shunt and external operational amplifier for a low current measurement ? by a shunt and external operational amplifier for a high current measurement. 1.3.1 voltage measurement one voltage channel, nominal voltage 230 v rms is used u peak-to-peak = 230 x ? ? 2 x 2 x 120% = 780 v (120% means a safety margin) resolution for 780 v: u lsb:peak-to-peak = 780 v x 0.02% = 0.16 v resolution for 230 v: u lsb:rms = 230 v x 0.02% = 0.055 v rms value 0.02% represents resolution and precision of the adc and is defined in section 1.1: stm32 adc parameters . the range where it is necessary to take care of the precision is between 80% to 115% (extended operating range) of the nominal voltage: u max = 230 v rms x 115% = 253 v rms u min = 230 v rms x 80% = 184 v rms for the lowest and highest voltage value th e precision of the voltage measurement is: error of u max = 0.055 v rms / 184 v rms = 0.030% error u min = 0.055 v rms / 253 v rms = 0.022% error values are much lower than prescribed by standards: 1% >> 0.030%. docid18304 rev 1 9/38 AN3322 watt-hour meter using the stm32 1.3.2 there are two curre nt ranges in the meter low gain range (lg): for 100 a range a resolution and accuracy with the 12-bit adc are: i max = 100 x 2 x 1.414 = 300 a peak-peak resolution in a for maximum value: 300a x 0.02% = 60 ma resolution in a for rms value: 100 a rms x 0.02% = 20 ma rms minimal error of measurement of current about: ? starting current: i st : 20 ma rms / 20 ma rms = 100% (no precision prescribed) ? minimum current: i min : 20 ma rms / 250 ma rms = 8% (1,5% prescribed) ? transition current: i tr : 20 ma rms / 500 ma rms = 4% (1% prescribed) ? reference current: i ref : 20 ma rms / 5 a rms = 0.4% (1% prescribed) ? maximum current: i max : 20 ma rms / 100 a rms = 0.02% (1% prescribed). error values are lower than prescribed standards in case of i ref and i max . lower currents i min and i tr cannot be measured within this lg range. high gain range (hg): for 10 a range a resolution and accuracy with the 12-bit adc are: i max = 10 x 2 x 1.414 = 28 a peak-peak resolution in a for maximum value: 28 a x 0.02% = 5.8 ma resolution in a for rms value: 10 a rms x 0.02% = 2 . 4 ma rms minimal error of measurement of current about: ? starting current: i st : 2.4 ma rms / 20 ma rms = 12% (no precision prescribed) ? minimum current: i min : 2.4 ma rms / 0.5 a rms = 0.48% (1,5% prescribed) ? transition current: i tr : 2.4 ma rms / 1 a rms = 0.24% (1% prescribed) ? reference current: i ref : 2.4 ma rms / 5 a rms = 0.048% (1% prescribed) ? maximum current: i max : 2.4 ma rms / 10 a rms = 0.024% (1% prescribed). error values are lower than prescribed standards in the case of i st , i min , i tr , and i ref . higher current i max cannot be measured within this hg range due to adc saturation. from the analysis performed in this section it can be seen that a system using one range for voltage and two ranges for cu rrent measurement can fulfill re quirements for the prescribed precision of the system. watt-hour meter using the stm32 AN3322 10/38 docid18304 rev 1 1.4 error sources on the stm32 meter there are a lot of sources of error when measuring current and voltage. they can be compensated in mostly all cases. 1. adc linearity: it limits total accura cy compensated by adc symmetry, see section 1.5 . 2. temperature: external voltage reference keeps adc conversion stable. 3. op amp and adc offsets: they are remo ved by ac coupling, calibration and digital filtering. 4. adc gain: it is compensated by calibration of the meter. 5. thermal noise present on the stm32 meter pcb board: see section 1.6 . 6. crosstalk between subsequent channel measurement: compensation is discussed in section 1.7 . 7. linearity of op amps: it is present, not compensated. selection of a good op amp is made. 8. phase shift between channels; the compensation is shown in section 2.7 . 1.5 linearity of stm32 adc and its inl reduction increasing the adc resolution does not result in higher accuracy since the techniques for obtaining higher resolution can reduce dnl (differential non-linearity) but not inl (integral non-linearity), see section 1.9: adc enhancement by oversampling technique . the linearity can be enhanced (inl reduction) from 0.7 to 0.17 lsb by the application of a simple technique to the hw and sw design. good knowledge of the adc is assumed for this technique, described in a sepa rate document supplied on request. 1.6 thermal noise present on stm32 meter platform the number of effective bits of a given a dc depends on the presence of noise in the system, namely on the pcb and microcontroller itself. if the noise is white gaussian noise (or similar), the number of effective bits can be recovered by oversampling and decimation mentioned in section 1.9 . figure 3 brings a direct noise measurement of the system. the single voltage level was connected to an input of the adc on the stm32 power meter board and a thousand samples were collected. figure 3 shows a histogram of the values taken. docid18304 rev 1 11/38 AN3322 watt-hour meter using the stm32 figure 3. typical thermal noise present on stm32 meter platform. histogram shows noise equivalent to white gaussian noise measured thermal noise (overall on the board) degrades the adc resolution and accuracy by 2 to 5 lsb. this means that the number of effective bits drops from 12 to less than 10. to recover from this situation it is necessary to use techniques for noeb (number of effective bits) enhancement, see section 1.9 . using oversampling requ ires, in some cases, the addition of the dithering signal. according to the noise level found in this system, it is not necessary to add the dithering signal. it's add ition does not bring any enhancement in the performance. 1.7 crosstalk between subseq uent multiplexed channels when reading more channels subsequently with maximum speed, values sampled from the subsequent channel may be influenced by the reading of the previous channel. this is called crosstalk between subsequent multiplexed channels. it is caused by the balance of external impedance and capacitance of a signal source and an internal impedance and capacitance of the sample and hold circuit in the given adc. the longer the sample and hold phase, the lower the correlation between subsequent samples. the technique to overcome this phenomenon presented in this section is based on reading every channel in the sequence twice and only th e second value is taken as the valid one. the error is reduced from 0.07% of adc full range to less than 0.0001%. the total conversion time is calculated as follows: equation 5 t conv = sampling time + 12.5 cycles example 2 with an adcclk = 14 mhz and a sampling time of 1.5 cycles: t conv_14mhz = 1.5 + 12.5 = 14 cycles = 1 s watt-hour meter using the stm32 AN3322 12/38 docid18304 rev 1 example 3 with the setting used in this application adcclk = 9 mhz and a sampling time of 1.5 cycles: t conv_9mhz = 1.5 + 12.5 = 14 cycles = 1.56 s t sampling_time = (1.5 cycles / 14 cycles) x 1.56 s = 167 ns figure 4. internal and external analog input pin capacitors and resistors equation 6 r 129 = 820 ? and r 137 = 820 ? (resistors between an output of op amp and an adc input) r ain = r 129 = r 137 = 820 ? r opamp = 80 ? ?? = rc = (r adc + r ain ) + r opam p) c = 1900 ? x 12 pf = 22.8 ns error % = 100 x exp. [-1 x (t sampling_time / ? )] from table 3 it is recommended to use every second value sampled on the same channel. this approach renders an error caused by this phenomenon negligible. table 2. stm32 adc input characteristics specifications symbol parameter conditio ns min. typ. max. unit radc sampling switch resistance 1 k ? cadc internal sample and hold capacitor 12 pf table 3. crosstalk between subsequent multiplexed channels number of the same channel readings [-] t conv [cycle / s] t sampling_time [ns] error [%] 1 1.5 / 1.55 167 0.07 2 3.0 / 3.10 334 0.00 3 4.5 / 4.65 501 0.00 4 6.0 / 6.20 668 0.00 b j e 4 5 . ' y y y 7 % % " * / y * - ? ? " 7 7 5 3 " * / $ q b s b t j u j d 7 " * / 7 7 5 3 " % $ $ " % $ c j u d p o w f s u f s 4 b n q m f b o e i p m e " % $ d p o w f s u f s docid18304 rev 1 13/38 AN3322 watt-hour meter using the stm32 1.8 power and energy measurem ent by stm32 embedded adc this section gives a complete overview of th e whole hardware and firmware of the stm32 power meter design. figure 5 shows a block diagram of the firmware, figure 6 and figure 7 show the physical realization of the current and voltage path. figure 5. complete block diagram of the algorithm used in stm32 power meter figure 5 shows the algorithm flow measurement. ? a fast measurement of the current and voltage with injected noise from surrounding environment triggered by network synchronized timer ? decimation of measured samples ? rms values computation ? use of a precise rtc clock source for energy computation. watt-hour meter using the stm32 AN3322 14/38 docid18304 rev 1 figure 6 shows the connectors of the power meter (cir cles with the numbers 1, 2, 3, and 4) and its connection to the network and load on the left side and voltage and current measurement circuit on the right side. voltage ci rcuitry depicted in this image shows that the voltage divider consists of resistors and a capacitor and it is referenced to u m (v mid ) voltage, which is half of +v ref (2.5 v). the voltage divider has a gain of 0.0027. u m (v mid ) voltage is also measured by the system. figure 6. voltage circuitry of stm32 meter 6 7 7 n j e " 7 / 7 4 4 7 / - _ 7 7 4 4 4 5 . " % $ j o 7 n j e " % $ j o 7 p m u b h f e j w j e f s h b j o y 6 f g g 7 6 7 6 n b y 7 6 h n b y 6 n b y 6 n j e 6 h n b y 7 6 n j o 6 n j e 6 h n b y 7 7 3 & |